//
//  AdvanceMVVMViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/8.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//

#import "AdvanceMVVMViewController.h"

#import "TableViewDataSource.h"
#import "TableViewDelegate.h"
#import "MVVMViewModel.h"
#import "MJRefresh.h"

@interface AdvanceMVVMViewController ()

@property (nonatomic, strong) TableViewDataSource *tableViewDataSource;
@property (nonatomic, strong) TableViewDelegate *tableViewDelegate;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) MVVMViewModel *viewModel;

@end

@implementation AdvanceMVVMViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.navigationItem.title = @"MVVM进阶";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) weakSelf = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefreshAction];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = UIColorFromRGB(0x999999);
    header.stateLabel.font = [UIFont systemFontOfSize:14];
    self.tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefreshAction];
    }];
    footer.stateLabel.textColor = UIColorFromRGB(0x999999);
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    [self.tableView.mj_header beginRefreshing];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)headerRefreshAction
{
    __weak typeof(self) weakSelf = self;
    [_viewModel headerRefreshRequestWithCallBack:^(NSArray * _Nonnull resultArray) {
        weakSelf.dataSource = (NSMutableArray *)resultArray;
        weakSelf.tableViewDataSource.dataSource = weakSelf.dataSource;
        weakSelf.tableViewDelegate.dataSource = weakSelf.dataSource;
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
        self.tableView.mj_footer.hidden = NO;
    }];
}

- (void)footerRefreshAction
{
    __weak typeof(self) weakSelf = self;
    [_viewModel footerRefreshRequestWithCallBack:^(NSArray * _Nonnull resultArray) {
        [weakSelf.dataSource addObjectsFromArray:resultArray];
        weakSelf.tableViewDataSource.dataSource = weakSelf.dataSource;
        weakSelf.tableViewDelegate.dataSource = weakSelf.dataSource;
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView reloadData];
    }];
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-BottomHeight-NavBarHeight) style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableViewDataSource = [[TableViewDataSource alloc] init];
        _tableViewDelegate = [[TableViewDelegate alloc] init];
        _tableView.dataSource = _tableViewDataSource;
        _tableView.delegate = _tableViewDelegate;
        _viewModel = [[MVVMViewModel alloc] init];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = @[].mutableCopy;
    }
    return _dataSource;
}

@end
